package com.hbsx.why.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.hbsx.why.DemoMain;
import com.hbsx.why.R;
import com.hbsx.why.TabMainActivity;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.views.CustomVideoView;

/**
 * Created by liub on 2017/7/4 17:27.
 */

public class WelcomeActivityVideo extends AppCompatActivity implements View.OnClickListener {
    private CustomVideoView videoview;
    private Button btn_start;
//    private ImageView ImgSound;
//    private boolean soundFlag = true;
//    private MediaPlayer mp;
    private float voilce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_video);
        initView();
        setStatusTint();
    }

    //    public void setContentView(View view, WindowManager.LayoutParams params) {
////**该方法需要在setContentView方法之后调用。重写BaseActivity中的setContentView(int layoutResID )方法，就可以实现全局地设置所有的Activity界面的状态栏颜色，如果项目中某个Activity需要设置不同的状态栏颜色，显式地调用该方法，并传入颜色值即可**
//
//        ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0).setFitsSystemWindows(true);
//    }
    //设置状态栏的颜色
    private void setStatusTint() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

//        SystemBarTintManager tintManager = new SystemBarTintManager(this);
//        tintManager.setStatusBarTintEnabled(false);
//        tintManager.setNavigationBarTintEnabled(false);
//        tintManager.setStatusBarTintResource(R.color.red);//通知栏所需颜色

    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);

    }

    /**
     * 初始化
     */
    private void initView() {
//        mp = new MediaPlayer();
//        AudioManager audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
//        mp.setAudioStreamType(audioManager.STREAM_MUSIC);
//        voilce = audioManager.getStreamVolume(audioManager.STREAM_MUSIC);
        btn_start = (Button) findViewById(R.id.btn_start);
//        ImgSound = (ImageView) findViewById(R.id.Image_song);
        btn_start.setOnClickListener(this);
//        ImgSound.setOnClickListener(this);
        videoview = (CustomVideoView) findViewById(R.id.videoview);
        //设置播放加载路径
        videoview.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video_demo1));
//        videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mediaPlayer) {
//                mp = mediaPlayer;
//            }
//        });
        //播放
        videoview.start();

        //循环播放
        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoview.start();
//                mp = mediaPlayer;
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                startActivity(new Intent(this, TabMainActivity.class));
                finish();
                break;
            case R.id.Image_song:
                ToastUtils.toastShort("-----voilce="+voilce);
//                if (soundFlag) {
//                    ImgSound.setBackgroundResource(R.mipmap.ic_sound_no);
//                    soundFlag = false;
//                    mp.setVolume(0f,0f);
//                } else {
//                    ImgSound.setBackgroundResource(R.mipmap.ic_sound_yes);
//                    soundFlag = true;
//                    mp.setVolume(voilce, voilce);
//                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoview = null;
//        mp = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }
}
